package com.building.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.building.common.R;
import com.building.enums.REnum;
import com.building.service.impl.BaseServiceImpl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import com.building.entity.ShoppingOrder;
import com.building.mapper.ShoppingOrderMapper;
import com.building.service.ShoppingOrderService;
/**
@author yinjiahui
@create 2021-04-11 17:11
*/
@Service
public class ShoppingOrderServiceImpl extends BaseServiceImpl<ShoppingOrderMapper, ShoppingOrder> implements ShoppingOrderService{

    @Override
    public int updateBatch(List<ShoppingOrder> list) {
        return baseMapper.updateBatch(list);
    }
    @Override
    public int updateBatchSelective(List<ShoppingOrder> list) {
        return baseMapper.updateBatchSelective(list);
    }
    @Override
    public int batchInsert(List<ShoppingOrder> list) {
        return baseMapper.batchInsert(list);
    }
    @Override
    public int insertOrUpdate(ShoppingOrder record) {
        return baseMapper.insertOrUpdate(record);
    }
    @Override
    public int insertOrUpdateSelective(ShoppingOrder record) {
        return baseMapper.insertOrUpdateSelective(record);
    }

    /**
     * 根据用户id查询订单中的商品
     *
     * @param userId
     * @return
     */
    @Override
    public List<ShoppingOrder> selectShoppingOrderByUserId(Integer userId) {
        QueryWrapper shoppingOrder = new QueryWrapper();
        shoppingOrder.eq("user_id",userId);
        return this.shoppingOrderMapper.selectList(shoppingOrder);
    }

    /**
     * 新增或修改订单
     *
     * @param shoppingOrder
     * @return
     */
    @Override
    public R insertOrUpdateShoppingOrderByShoppingOrderId(ShoppingOrder shoppingOrder) {
        if (shoppingOrder.getShoppingOrderId() == null){
            this.shoppingOrderMapper.insert(shoppingOrder);
            return R.result(REnum.SUCCESS_INSERT);
        }else {
            this.shoppingOrderMapper.updateById(shoppingOrder);
            return R.result(REnum.SUCCESS_UPDATE);
        }
    }

    /**
     * 根据订单id和用户id删除订单中的商品
     *
     * @param shoppingOrderId
     * @param userId
     * @return
     */
    @Override
    public void deleteShoppingOrderByShoppingOrderIdAndUserId(Integer shoppingOrderId,Integer userId) {
        QueryWrapper del = new QueryWrapper();
        del.eq("user_id",userId);
        del.eq("shopping_order_id",shoppingOrderId);

        this.shoppingOrderMapper.delete(del);
    }
}
